<template>
  <div>
    <div>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="pageInfo.records"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            @current-change="handleCurrentChange"
          >

            <!-- <el-table-column align="center" type="selection" :selectable='checkboxT' width="45"></el-table-column> -->
            <el-table-column type="index" width="50" align="center" />

            <el-table-column align="center" label="单位名称">
              <template slot-scope="scope">
                {{ scope.row.name }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="状态">
              <template slot-scope="scope">
                {{ scope.row.contradictionStatus | statusFilter }}
              </template>
            </el-table-column>

          </el-table>
        </el-col>
        <!-- <el-col :span="12">
          <label>备注：</label>
          <el-input
            type="textarea"
            :autosize="{ minRows: 10, maxRows: 20}"
            placeholder="请输入内容"
            v-model="form.description">
          </el-input>
        </el-col> -->
      </el-row>

      <!-- 分页 -->
      <el-row style="margin-top: 15px;">
        <el-col style="text-align: center;">
          <el-pagination
            background
            :current-page="pageInfo.current"
            :page-size="pageInfo.size"
            layout="total, prev, pager, next, jumper"
            :total="pageInfo.total"
            @current-change="getInstitutionListPage"
          />
        </el-col>
      </el-row>
    </div>

    <div style="display: flex; align-items:center; justify-content:center; margin-top: 20px;">
      <el-button :loading="buttonLoading" type="primary" @click="onSubmit">推送</el-button>
      <el-button @click="cancel">关闭</el-button>
    </div>

  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs';
export default {
  filters: {
    statusFilter(status) {
      if (status === 0) {
        return '已推送（待解决）';
      }
      if (status === 1) {
        return '已推送（已解决）';
      }
      if (status === 2) {
        return '已推送（未解决）';
      }
    }
  },
  components: {

  },
  props: ['selectedContradictionId'],
  data() {
    return {
      buttonLoading: false,
      pageInfo: {},
      listLoading: true,
      searchData: {
        currentPage: 1
      },
      loading: false,
      contradictionRecordList: [],
      form: {
        institutionIds: []
      }
    }
  },
  created() {
    this.getInstitutionListPage(1)
  },
  mounted() {

  },
  methods: {
    getInstitutionListPage(currentPage) {
      this.listLoading = true

      this.searchData.currentPage = currentPage;
      this.searchData.contradictionId = this.selectedContradictionId;
      request({
        url: '/api/institution/getInstitutionListPage',
        method: 'get',
        params: this.searchData
      }).then((res) => {
        this.pageInfo = res.result;
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 多选框
    handleCurrentChange(currentRow, oldCurrentRow) {
      if (currentRow != null) {
        this.form.institutionIds = [currentRow.id];
      }
      // if (arr.length > 1) {
      //   this.$refs.multipleTable.toggleRowSelection(row);
      //   this.$message({
      //     message: '只能选择一个单位',
      //     type: 'warning'
      //   })
      // } else {
      //     var localArr = [];
      //     arr.forEach((item) => {
      //       localArr.push(item.id);
      //     })
      //     this.form.institutionIds = localArr;
      // // }
    },
    // table复选框
    checkboxT(row, index) {
      if (row.contradictionStatus !== null) {
        return false; // 禁用
      } else {
        return true; // 不禁用
      }
    },
    onSubmit() {
      if (this.form.institutionIds == null || this.form.institutionIds.length === 0) {
        this.$message({
          message: '请选择单位',
          type: 'warning'
        })
        return;
      }

      this.buttonLoading = true;
      this.form.contradictionId = this.selectedContradictionId;
      request({
        url: '/api/contradictionRecord/pushContradiction',
        method: 'post',
        data: qs.stringify(this.form, { arrayFormat: 'brackets' })
      }).then((res) => {
        this.buttonLoading = false;
        if (res.resultCode == 200) {
          this.$message(res.resultCodeDesc)
          this.$emit('refresh');
          this.$emit('closeDialog', false);
        } else {
          this.$message({
            message: res.resultCodeDesc,
            type: 'warning',
            duration: 5 * 1000
          })
        }
      }).catch(_ => {
        this.buttonLoading = false;
      })
    },
    cancel() {
      this.$emit('closeDialog');
    }
  }
};
</script>
<style>
.el-table__body tr.current-row > td {
  background-color: #a0cfff !important;
}
</style>
